﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using LZL.Web;
using SharpSvn;

namespace TestSharpSvn
{
  /// <summary>
  /// TestSvnHome 的摘要说明
  /// </summary>
  public class TestSvnHome : BaseActionHttpHandler
  {
    private static readonly IDictionary<string, System.Reflection.MethodInfo> services =
      GetServices(typeof(TestSvnHomeService));
    protected override IDictionary<string, System.Reflection.MethodInfo> Services
    {
      get { return services; }
    }

    protected override BaseActionService ServicesClassType
    {
      get { return new TestSvnHomeService(Context); }
    }
  }

  internal class TestSvnHomeService : BaseActionService
  {
    string path = "e:\\worktest\\svn\\test\\";
    public TestSvnHomeService(HttpContext context) : base(context) { }

    public void CheckOut()
    {
      using (SvnClient client = new SvnClient())
      {
        string configPath = Path.Combine(Path.GetTempPath(), "sharpsvn");
        if (!Directory.Exists(configPath))
        {
          Directory.CreateDirectory(configPath);
        }
        client.LoadConfiguration(configPath, true);
        client.Authentication.DefaultCredentials = new NetworkCredential("guest", "");
        // Checkout the code to the specified directory
        client.CheckOut(new Uri("https://ctf.open.collab.net/svn/repos/sharpsvn/"), path);

        // Update the specified working copy path to the head revision
        client.Update(path);
        SvnUpdateResult result;
        client.Update(path, out result);


        // client.Move("c:\\sharpsvn\\from.txt", "c:\\sharpsvn\\new.txt");

        // Commit the changes with the specified logmessage
        // SvnCommitArgs ca = new SvnCommitArgs();
        //  ca.LogMessage = "Moved from.txt to new.txt";
        // client.Commit("c:\\sharpsvn", ca);
        client.Commit(path);
      }
    }

    public void addFile()
    {
      var p = Path.Combine(path, DateTime.Now.ToString("yyyyMMddHHmmss"), ".txt");
      File.WriteAllText(p, "1");
      using (var client = SvnHelper.GetClient())
      {
        client.Add(p,new SvnAddArgs()
        {
          AddParents = true,
          Depth = SvnDepth.Empty
        });
      }
    }
  }
}